package LS;

import java.sql.*;
import java.text.*;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.logging.Log;
import org.springframework.beans.factory.annotation.*;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import LS.*;

@Controller
public class YController {

	@Autowired
	private YMapper YMapper;

	@RequestMapping("/m/equipSearch.do")
	public ModelAndView equipSearch(@RequestParam(required = false)String depart_id, String item_id,String model_name, String condition, String text, HttpSession session) {
		ModelAndView result = new ModelAndView("managerPage/equipSearch");

		text = "%" + text + "%";
		result.addObject("equipSearch", YMapper.equipSearch(depart_id, item_id, condition, text));
		result.addObject("depart", YMapper.selectDepart());
		result.addObject("item", YMapper.selectItem());
		return result;
	}
	
	@RequestMapping("/m/modifyEquip.do")
	public ModelAndView modifyEquip(@RequestParam(required = false) String equipment_id) {
		ModelAndView result = new ModelAndView("managerPage/modifyEquip");
		
		int depart = YMapper.selectEquipmentsIdCheck(equipment_id).getDepartments_id();
		
		result.addObject("depart", YMapper.selectDepartById(depart));
		result.addObject("using", YMapper.selectUsing());
		result.addObject("item", YMapper.selectItem());
		result.addObject("equip", YMapper.selectEquipmentsIdCheck(equipment_id));
		result.addObject("stuInfo", YMapper.stuInfo(equipment_id));
		
		return result;
	}
	
	@RequestMapping("/m/modifyEquipBtn.do")
	public ModelAndView modifyEquipBtn(@RequestParam(required = false) String equipment_id,
			String model_name, String price, String using_id, String item_id, String note,
			String what, String equipment_state
			) {
		
		ModelAndView result = new ModelAndView("managerPage/modifyEquipBtn");
		if(what.equalsIgnoreCase("1")){
			System.out.println("equipment_state: " + equipment_state);
			System.out.println("equipment_state: " + equipment_state);
			
			YMapper.updateEquip(equipment_id, model_name, price, using_id, item_id, note);
			YMapper.updateEquipState(equipment_state, equipment_id);
//			return new ModelAndView("redirect:/modifyEquip.do?equipment_id="+equipment_id);
			return result;
		}
		else{
			YMapper.deleteEquip(equipment_id);
			return result;
		}
	}
	
	@RequestMapping("/m/modifyEquipDeleteBtn.do")
	public ModelAndView modifyEquipDeleteBtn(@RequestParam(required = false) String equipment_id) {
		return new ModelAndView("redirect:/m/modifyEquip.do?equipment_id="+equipment_id);
	}
	
	@RequestMapping("/m/return.do")
	public ModelAndView returnEquip(@RequestParam(required = false) String equipment_id) {
		ModelAndView result = new ModelAndView("managerPage/return");
		
		if(equipment_id != null){
			equipment_id = "%" + equipment_id + "%";
			result.addObject("list", YMapper.selectForReturn(equipment_id));
		}
		
		if(equipment_id==""){
			result.addObject("list",YMapper.selectForReturn(equipment_id));
		}
		
		return result;
	}
	
	@RequestMapping("/m/returnSmartpop.do")
	public ModelAndView returnSmartpop(@RequestParam(required = false) String lending_id) {
		ModelAndView result = new ModelAndView("managerPage/returnSmartpop");
		System.out.println(lending_id);
		if(!("null".equalsIgnoreCase(lending_id))){
			int l = Integer.parseInt(lending_id);
			result.addObject("info", YMapper.selectForReturnUserInfo(l));
			return result;
		}else{
			return new ModelAndView("redirect:/m/erro.do");
		}
	}
	@RequestMapping("/m/erro.do")
	public ModelAndView returnSmartpopBtn() {
		ModelAndView result = new ModelAndView("managerPage/erro");
		return result;
	}
	@RequestMapping("/m/returnSmartpopBtn.do")
	public ModelAndView returnSmartpopBtn(@RequestParam(required = false) String lending_id) {
		ModelAndView result = new ModelAndView("managerPage/returnSmartpopBtn");
		YMapper.returnFinal(lending_id);
		return result;
	}
	
	@RequestMapping("/m/manageManager.do")
	public ModelAndView manageManager(String idcheck) {
		ModelAndView result = new ModelAndView("managerPage/manageManager");
		
		result.addObject("selectManager",YMapper.selectManager());

		return result;
	}
	
	@RequestMapping("/m/addManager.do")
	public ModelAndView addManager(@RequestParam(required = false) String id, String pw1, String pw2, String name ) {
		if(YMapper.selectUserById(id)>0){
			return new ModelAndView("redirect:/m/manageManager.do?check=no");
		}
		
		else if(pw1.equalsIgnoreCase(pw2)){
			YMapper.addManager(id, pw1, name);
			return new ModelAndView("redirect:/m/manageManager.do");
		}
		
		else{
			return new ModelAndView("redirect:/m/manageManager.do?check=no");
		}
	}
	
	@RequestMapping("/m/deleteManager.do")
	public ModelAndView deleteManager(@RequestParam(required = false) String user_id) {
		YMapper.deleteManager(user_id);
		
		return new ModelAndView("redirect:/m/manageManager.do");
	}
}
